package com.gec.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.pojo.TUser;
import com.gec.model.vo.TUserQueryVo;
import com.gec.system.Service.TUserService;
import com.gec.system.util.MD5Helper;
import com.gec.system.util.RandomIdUtil;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "前台用户管理控制器")
@RestController
@RequestMapping("/admin/system/tUser")
public class TUserController {
    @Autowired
    private TUserService tUserService;

    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectTUserPageByVo(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      TUserQueryVo tUserQueryVo) {
        IPage<TUser> iPage = new Page<>(page, limit);
        iPage = tUserService.selectPage(iPage, tUserQueryVo);
        return Result.ok(iPage);
    }

    @ApiOperation("修改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status) {
        tUserService.updateStatus(id, status);
        return Result.ok();
    }

//    @ApiOperation("axios随机id")
//    @GetMapping("/randomId/{id}")
//    public Result randomId(@PathVariable Long id) {
//        TUser tid = tUserService.getById(id);
//        if (tid == null){
//            return Result.ok(0);
//        }else {
//            return Result.ok(1);
//        }
//    }

//    @ApiOperation("检测随机user_id")
//    @GetMapping("/getRandomUserId/{user_id}")
//    public Result getRandomUserId(@PathVariable Long user_id) {
//        TUser uid = tUserService.getByUserId(user_id);
//        if (uid == null){
//            return Result.ok(0);
//        }else {
//            return Result.ok(1);
//        }
//    }

    @ApiOperation("添加用户")
    @PostMapping("/addTUser")
    public Result addTUser(@RequestBody TUser tUser) {
        //MD5工具类加密
        String passwordWithMD5 = MD5Helper.encrypt(tUser.getPassword());
        tUser.setPassword(passwordWithMD5);

        Long uid;
        String id;
        // 随机id
        while (true) {
            id = RandomIdUtil.randomId();
            TUser userById = tUserService.getById(id);
            if (userById == null){
                break;
            }
        }
        System.out.println("用户id：" + id);
        tUser.setId(id);

        //随机userId
        while (true) {
            uid = Math.round(Math.random()*1000000000);
            TUser tu = tUserService.getByUserId(uid);
            if (tu == null) {
                break;
            }
        }
        System.out.println("uid：" + uid);
        tUser.setYxId(uid);
        System.out.println("tUserId："+tUser.getId());
        boolean b = tUserService.save(tUser);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @ApiOperation("根据id 去获取一个用户")
    @GetMapping("/findTUserById/{id}")
    public Result findTUserById(@PathVariable Long id) {
        TUser tUser = this.tUserService.getById(id);
        return Result.ok(tUser);
    }

    @ApiOperation("修改实现")
    @PostMapping("/updateTUser")
    public Result updateTUser(@RequestBody TUser tUser) {
        boolean b = tUserService.updateById(tUser);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("删除实现")
    @DeleteMapping("/removeTUserById/{id}")
    public Result removeTUserById(@PathVariable Long id) {
        boolean b = tUserService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}
